<html>
<head>
  <title>Evernote Export</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/601568 (zh-CN, DDL); Windows/10.0.0 (Win64); EDAMVersion=V2;"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="3108"/>

<div>
<span><div style="padding:14px 18px 10px 18px;border-radius: 4px; text-align:start;background-color:#f7f7f7;margin-bottom: 0"><span style="margin:0px;"><span style="background-color: rgb(247, 247, 247);"><span style="background-color: rgb(247, 247, 247); font-size: 10pt; color: rgb(0, 181, 72);">速读摘要</span></span></span></div><div style="padding:0px 18px;text-align:start;background-color:#f7f7f7;margin-top: 0;margin-bottom: 0"><span style="background-color: rgb(247, 247, 247);"><span style="background-color: rgb(247, 247, 247); font-size: 10pt; color: rgb(0, 0, 0);">Java语言为了解决并发编程中存在的原子性、可见性和有序性问题，提供了一系列和并发处理相关的关键字，比如synchronized、volatile、final、concurren包等。实际上还是有重排的，只不过我们无须关心这种重排的干扰。由于synchronized修饰的代码，同一时间只能被同一线程访问。前面介绍了synchronized的用法、原理以及对并发编程的作用。</span></span></div><div style="padding:8px 0px;margin:8px auto;max-width:700px;"><div style="margin: 0px; padding: 0px;"><div style="margin: 0px; padding: 0px;"><div style="margin: 0px; padding: 0px; position: relative;"><div style="margin: 0px; padding: 0px; overflow: hidden; word-wrap: break-word; text-align: justify; position: relative; z-index: 0;"><div style="margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">在《深入理解Java虚拟机》中，有这样一段话：</span></div><blockquote style="background: rgb(242, 247, 251); border-left: 6px solid rgb(220, 230, 240); margin: 1em 0px; padding: 15px 15px 15px 1rem; overflow: auto; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin: 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="font-size: 10pt; color: rgb(129, 145, 152);">关键字在需要原子性、可见性和有序性这三种特性的时候都可以作为其中一种解决方案，看起来是“万能”的。的确，大部分并发控制操作都能使用synchronized来完成。</span></font></div></blockquote><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">海明威在他的《午后之死》说过的：“冰山运动之雄伟壮观，是因为他只有八分之一在水面上。”</span></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">对于程序员来说，</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">只是个关键字而已，用起来很简单。之所以我们可以在处理多线程问题时可以不用考虑太多，就是因为这个关键字帮我们屏蔽了很多细节。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">那么，本文就围绕</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">展开，主要介绍其用法、原理，以及如何提供原子性、可见性和有序性保障的等。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important;margin-top:10px;margin-bottom:10px"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important;margin-left:0.8em;float:left"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><font style="font-size: 10pt;"><span style="margin: 0.5em 0px 0px; padding: 0px; display: inline-block; vertical-align: top; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(0, 0, 0);">synchronized的用法</span></span></font></div></div><br/></div></div></div></div></div></div></div></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">是Java提供的一个并发控制的关键字。主要有两种用法，分别是同步方法和同步代码块。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(51, 51, 51);">也就是说，</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(51, 51, 51);">既可以修饰方法也可以修饰代码块。代码如下：</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin: 0px 2px; letter-spacing: 0px; border-radius: 0px; background: rgb(40, 43, 46); padding: 0.5em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: normal !important; overflow: auto !important;"><div><span style="margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important;"><span style="font-size: 10pt; color: rgb(128, 128, 128); line-height: 18px;">/**</span></span></div><div><span style="margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important;"><span style="font-size: 10pt; color: rgb(128, 128, 128); line-height: 18px;"> * @author Hollis 18/08/04.</span></span></div><div><span style="margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important;"><span style="font-size: 10pt; color: rgb(128, 128, 128); line-height: 18px;"> */</span></span></div><div><font style="font-size: 10pt;"><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">public</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;"> </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">class</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;"> </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(165, 218, 45); line-height: 18px;">SynchronizedDemo</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;"> {</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">     </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(128, 128, 128); line-height: 18px;">//同步方法</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">    </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">public</span><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: inherit; word-break: inherit; font-size: 10pt; color: rgb(248, 35, 117); line-height: 18px;"> synchronized </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">void</span><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: inherit; word-break: inherit; font-size: 10pt; color: rgb(248, 35, 117); line-height: 18px;"> </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(165, 218, 45); line-height: 18px;">doSth</span><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: inherit; word-break: inherit; font-size: 10pt; color: rgb(248, 35, 117); line-height: 18px;">()</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">{</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">        System.</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">out</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">.println(</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(238, 220, 112); line-height: 18px;">&quot;Hello World&quot;</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">);</span></font></div><div><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">    }</span></div><div><font style="font-size: 10pt;"><br style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"/></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">    </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(128, 128, 128); line-height: 18px;">//同步代码块</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">    </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">public</span><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: inherit; word-break: inherit; font-size: 10pt; color: rgb(248, 35, 117); line-height: 18px;"> </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">void</span><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: inherit; word-break: inherit; font-size: 10pt; color: rgb(248, 35, 117); line-height: 18px;"> </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(165, 218, 45); line-height: 18px;">doSth1</span><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: inherit; word-break: inherit; font-size: 10pt; color: rgb(248, 35, 117); line-height: 18px;">()</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">{</span></font></div><div><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">        synchronized (SynchronizedDemo.class){</span></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">            System.</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">out</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">.println(</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(238, 220, 112); line-height: 18px;">&quot;Hello World&quot;</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">);</span></font></div><div><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">        }</span></div><div><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">    }</span></div><div><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">}</span></div></div></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">被</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">修饰的代码块及方法，在同一时间，只能被单个线程访问。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important;margin-top:10px;margin-bottom:10px"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important;margin-left:0.8em;float:left"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><font style="font-size: 10pt;"><span style="margin: 0.5em 0px 0px; padding: 0px; display: inline-block; vertical-align: top; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><b>synchronized的实现原理</b></span></span></font></div></div><b><br/></b></div></div></div></div></div></div></div></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">，是Java中用于解决并发情况下数据同步访问的一个很重要的关键字。当我们想要保证一个共享资源在同一时间只会被一个线程访问到时，我们可以在代码中使用</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">关键字对类或者对象加锁。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">在</span><a href="http://mp.weixin.qq.com/s?__biz=MzI3NzE0NjcwMg==&amp;mid=2650120537&amp;idx=1&amp;sn=f56201217c0ca6fde45ee12965b56296&amp;chksm=f36bbc78c41c356ee363367addcdc0b311afb2f9df86a7ee20d21348b3332fd64f273d6028ca&amp;scene=21#wechat_redirect" style="margin: 0px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(87, 107, 149); text-decoration: none;" target="_blank">深入理解多线程（一）——Synchronized的实现原理</a><span style="font-size: 10pt; color: rgb(51, 51, 51);">中我曾经介绍过其实现原理，为了保证知识的完整性，这里再简单介绍一下，详细的内容请去原文阅读。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">我们对上面的代码进行</span><a href="http://mp.weixin.qq.com/s?__biz=MzI3NzE0NjcwMg==&amp;mid=2650120609&amp;idx=1&amp;sn=5659f96310963ad57d55b48cee63c788&amp;chksm=f36bbc80c41c3596a1e4bf9501c6280481f1b9e06d07af354474e6f3ed366fef016df673a7ba&amp;scene=21#wechat_redirect" style="margin: 0px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(87, 107, 149); text-decoration: none;" target="_blank">反编译</a><span style="font-size: 10pt; color: rgb(51, 51, 51);">，可以得到如下代码：</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin: 0px 2px; letter-spacing: 0px; border-radius: 0px; background: rgb(40, 43, 46); padding: 0.5em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: normal !important; overflow: auto !important;"><div><font style="font-size: 10pt;"><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">public</span><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: inherit; word-break: inherit; font-size: 10pt; color: rgb(248, 35, 117); line-height: 18px;"> synchronized </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">void</span><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: inherit; word-break: inherit; font-size: 10pt; color: rgb(248, 35, 117); line-height: 18px;"> </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(165, 218, 45); line-height: 18px;">doSth</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(255, 152, 35); line-height: 18px;">()</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">;</span></font></div><div><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">    descriptor: ()V</span></div><div><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">    flags: ACC_PUBLIC, ACC_SYNCHRONIZED</span></div><div><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">    Code:</span></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">      </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">stack</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">=</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">2</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">, locals=</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">1</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">, args_size=</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">1</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">         </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">0</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: getstatic     #</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">2</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">                  </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(128, 128, 128); line-height: 18px;">// Field java/lang/System.out:Ljava/io/PrintStream;</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">         </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">3</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: ldc           #</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">3</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">                  </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(128, 128, 128); line-height: 18px;">// String Hello World</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">         </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">5</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: invokevirtual #</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">4</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">                  </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(128, 128, 128); line-height: 18px;">// Method java/io/PrintStream.println:(Ljava/lang/String;)V</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">         </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">8</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">return</span></font></div><div><font style="font-size: 10pt;"><br style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"/></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">  </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">public</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;"> </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">void</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;"> doSth1();</span></font></div><div><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">    descriptor: ()V</span></div><div><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">    flags: ACC_PUBLIC</span></div><div><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">    Code:</span></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">      </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">stack</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">=</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">2</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">, locals=</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">3</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">, args_size=</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">1</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">         </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">0</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: ldc           #</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">5</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">                  </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(128, 128, 128); line-height: 18px;">// class com/hollis/SynchronizedTest</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">         </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">2</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: dup</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">         </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">3</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: astore_1</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">         </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">4</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: monitorenter</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">         </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">5</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: getstatic     #</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">2</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">                  </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(128, 128, 128); line-height: 18px;">// Field java/lang/System.out:Ljava/io/PrintStream;</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">         </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">8</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: ldc           #</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">3</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">                  </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(128, 128, 128); line-height: 18px;">// String Hello World</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">        </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">10</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: invokevirtual #</span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">4</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">                  </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(128, 128, 128); line-height: 18px;">// Method java/io/PrintStream.println:(Ljava/lang/String;)V</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">        </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">13</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: aload_1</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">        </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">14</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: monitorexit</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">        </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">15</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">goto</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">          </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">23</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">        </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">18</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: astore_2</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">        </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">19</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: aload_1</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">        </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">20</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: monitorexit</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">        </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">21</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: aload_2</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">        </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">22</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: athrow</span></font></div><div><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">        </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(174, 135, 250); line-height: 18px;">23</span><span style="font-size: 10pt; color: rgb(169, 183, 198); line-height: 18px;">: </span><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: inherit !important; word-break: inherit !important; color: rgb(248, 35, 117); line-height: 18px;">return</span></font></div></div></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">通过反编译后代码可以看出：</span></div><div style="margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt; background-color: rgb(255, 250, 165);-evernote-highlight:true;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">对于同步方法，JVM采用</span><span style="background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">ACC_SYNCHRONIZED</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">标记符来实现同步。 </span></font></div><div style="margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">对于同步代码块。JVM采用</span><span style="background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">monitorenter</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">、</span><span style="background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">monitorexit</span></span><span style="font-size: 10pt; color: rgb(51, 51, 51);"><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">两个指令来实现同步</span><span>。</span></span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">在The Java® Virtual Machine Specification中有关于同步方法和同步代码块的实现原理的介绍，我翻译成中文如下：</span></div><blockquote style="background: rgb(242, 247, 251); border-left: 6px solid rgb(220, 230, 240); margin: 1em 0px; padding: 15px 15px 15px 1rem; overflow: auto; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin: 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><b><span style="font-size: 10pt;">方法级的同步是隐式的。同步方法的常量池中会有一个</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt;">ACC_SYNCHRONIZED</span><span style="font-size: 10pt;">标志。当某个线程要访问某个方法的时候，会检查是否有</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt;">ACC_SYNCHRONIZED</span><span style="font-size: 10pt;">，如果有设置，则需要先获得监视器锁，然后开始执行方法，方法执行之后再释放监视器锁。这时如果其他线程来请求执行方法，会因为无法获得监视器锁而被阻断住。值得注意的是，如果在方法执行过程中，发生了异常，并且方法内部并没有处理该异常，那么在异常被抛到方法外面之前监视器锁会被自动释放。</span></b></font></div><div style="margin: 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><b><br style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"/></b></font></div><div style="margin: 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><b><span style="font-size: 10pt;">同步代码块使用</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt;">monitorenter</span><span style="font-size: 10pt;">和</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt;">monitorexit</span><span style="font-size: 10pt;">两个指令实现。可以把执行</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt;">monitorenter</span><span style="font-size: 10pt;">指令理解为加锁，执行</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt;">monitorexit</span><span style="font-size: 10pt;">理解为释放锁。 每个对象维护着一个记录着被锁次数的计数器。未被锁定的对象的该计数器为0，当一个线程获得锁（执行</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt;">monitorenter</span><span style="font-size: 10pt;">）后，该计数器自增变为 1 ，当同一个线程再次获得该对象的锁的时候，计数器再次自增。当同一个线程释放锁（执行</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt;">monitorexit</span><span style="font-size: 10pt;">指令）的时候，计数器再自减。当计数器为0的时候。锁将被释放，其他线程便可以获得锁。</span></b></font></div></blockquote><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">无论是</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">ACC_SYNCHRONIZED</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">还是</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">monitorenter</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">、</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">monitorexit</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">都是基于Monitor实现的，在Java虚拟机(HotSpot)中，Monitor是基于C++实现的，由ObjectMonitor实现。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">ObjectMonitor类中提供了几个方法，如</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">enter</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">、</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">exit</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">、</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">wait</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">、</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">notify</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">、</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">notifyAll</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">等。</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">sychronized</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">加锁的时候，会调用objectMonitor的enter方法，解锁的时候会调用exit方法。（关于Monitor详见</span><a href="http://mp.weixin.qq.com/s?__biz=MzI3NzE0NjcwMg==&amp;mid=2650120784&amp;idx=1&amp;sn=3436c978f0d7ab3bb672d03689518902&amp;chksm=f36bbf71c41c36672a3a6a7edebe0b913f2f5cf33d75d594d228f086ec7bdb9e253ab0beeae4&amp;scene=21#wechat_redirect" style="margin: 0px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(87, 107, 149); text-decoration: none;" target="_blank">深入理解多线程（四）—— Moniter的实现原理</a><span style="font-size: 10pt; color: rgb(51, 51, 51);">）</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important;margin-top:10px;margin-bottom:10px"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important;margin-left:0.8em;float:left"><div><br/></div><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><font style="font-size: 10pt;"><span style="margin: 0.5em 0px 0px; padding: 0px; display: inline-block; vertical-align: top; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(0, 0, 0);"><b>synchronized与原子性</b></span></span></font></div><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><font style="font-size: 10pt;"><span style="margin: 0.5em 0px 0px; padding: 0px; display: inline-block; vertical-align: top; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(0, 0, 0);"><br/></span></span></font></div></div></div><div style="margin: 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="margin: 0px; padding: 0px; letter-spacing: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(0, 0, 0);">原子性是指一个操作是不可中断的，要全部执行完成，要不就都不执行。</span></span></div></div></div></div></div></div></div></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">我们在</span><a href="http://mp.weixin.qq.com/s?__biz=MzI3NzE0NjcwMg==&amp;mid=2650121739&amp;idx=1&amp;sn=7b60f56d408457f34d5fc0b200ed32bb&amp;chksm=f36bbb2ac41c323c000e2a51e76c627d17f4759d3a54c520a073506ef3de0c46e196f11cd287&amp;scene=21#wechat_redirect" style="margin: 0px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(87, 107, 149); text-decoration: none;" target="_blank">Java的并发编程中的多线程问题到底是怎么回事儿</a><span style="font-size: 10pt; color: rgb(51, 51, 51);">中分析过：</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">线程是CPU调度的基本单位。CPU有时间片的概念，会根据不同的调度算法进行线程调度。当一个线程获得时间片之后开始执行，在时间片耗尽之后，就会失去CPU使用权。所以在多线程场景下，由于时间片在线程间轮换，就会发生原子性问题。</span></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">在Java中，为了保证原子性，提供了两个高级的字节码指令</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">monitorenter</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">和</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">monitorexit</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">前面介绍过，这两个字节码指令，在Java中对应的关键字就是</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">通过</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">monitorenter</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">和</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">monitorexit</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">指令，可以保证被</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">修饰的代码在同一时间只能被一个线程访问，在锁未释放之前，无法被其他线程访问到。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(51, 51, 51);">因此，在Java中可以使用</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(51, 51, 51);">来保证方法和代码块内的操作是原子性的。</span></font></div><blockquote style="background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-left: 6px solid rgb(220, 230, 240); margin: 1em 0px; padding: 15px 15px 15px 1rem; overflow: auto; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin: 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><b><span style="background-color: rgb(242, 247, 251); font-size: 10pt;">线程1在执行</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt;">monitorenter</span><span style="font-size: 10pt;"><span style="background-color: rgb(242, 247, 251);">指令的时候，会对Monitor进行加锁，加锁后其他线程无法获得锁，除非线程1主动解锁。即使在执行过程中，由于某种原因，比如</span><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">CPU时间片用完，线程1放弃了CPU，但是，他并没有进行解锁。而由于</span></span><span style="background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; background-color: rgb(255, 250, 165);-evernote-highlight:true;">synchronized</span><span style="font-size: 10pt;"><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">的锁是可重入的</span><span style="background-color: rgb(242, 247, 251);">，下一个时间片还是只能被他自己获取到，还是会继续执行代码。直到所有代码执行完。这就保证了原子性。</span></span></b></font></div></blockquote><div style="background-color: rgb(255, 255, 255); margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important;margin-top:10px;margin-bottom:10px"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important;margin-left:0.8em;float:left"><div><br/></div><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><font style="font-size: 10pt;"><span style="margin: 0.5em 0px 0px; padding: 0px; display: inline-block; vertical-align: top; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(0, 0, 0);"><b>synchronized与可见性</b></span></span></font></div><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><font style="font-size: 10pt;"><span style="margin: 0.5em 0px 0px; padding: 0px; display: inline-block; vertical-align: top; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(0, 0, 0);"><br/></span></span></font></div></div><br/></div><div style="margin: 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="margin: 0px; padding: 0px; letter-spacing: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(0, 0, 0);">可见性是指当多个线程访问同一个变量时，一个线程修改了这个变量的值，其他线程能够立即看得到修改的值。</span></span></div></div></div></div></div></div></div></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">我们在</span><a href="http://mp.weixin.qq.com/s?__biz=MzI3NzE0NjcwMg==&amp;mid=2650121599&amp;idx=1&amp;sn=42b2cfabfb3057ac6c09026a8b9656cd&amp;chksm=f36bb85ec41c31489e461a53e78f2959f0224c87c312724f420265b70e67e4efdae2331155aa&amp;scene=21#wechat_redirect" style="margin: 0px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: normal; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(87, 107, 149); text-decoration: none;" target="_blank">再有人问你Java内存模型是什么，就把这篇文章发给他</a><span style="font-size: 10pt; color: rgb(51, 51, 51);">中分析过：</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">Java内存模型规定了所有的变量都存储在主内存中，每条线程还有自己的工作内存，线程的工作内存中保存了该线程中是用到的变量的主内存副本拷贝，线程对变量的所有操作都必须在工作内存中进行，而不能直接读写主内存。不同的线程之间也无法直接访问对方工作内存中的变量，线程间变量的传递均需要自己的工作内存和主存之间进行数据同步进行。所以，就可能出现线程1改了某个变量的值，但是线程2不可见的情况。</span></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">前面我们介绍过，被</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">修饰的代码，在开始执行时会加锁，执行完成后会进行解锁。</span></font></div><div style="margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(51, 51, 51);"><span style="background-color: rgb(255, 255, 255);">而为了保证可见性，有一条规则是这样的：</span><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">对一个变量解锁之前，必须先把此变量同步回主存中。这样解锁后，后续线程就可以访问到被修改后的值。</span></span></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">所以，synchronized关键字锁住的对象，其值是具有可见性的。</span></span></div><div style="background-color: rgb(255, 255, 255); margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important;margin-top:10px;margin-bottom:10px"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important;margin-left:0.8em;float:left"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><font style="font-size: 10pt;"><span style="margin: 0.5em 0px 0px; padding: 0px; display: inline-block; vertical-align: top; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(0, 0, 0);"><b>synchronized与有序性</b></span></span></font></div><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><font style="font-size: 10pt;"><span style="margin: 0.5em 0px 0px; padding: 0px; display: inline-block; vertical-align: top; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(0, 0, 0);"><br/></span></span></font></div></div><br/></div><div style="margin: 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="margin: 0px; padding: 0px; letter-spacing: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(0, 0, 0);">有序性即程序执行的顺序按照代码的先后顺序执行。</span></span></div></div></div></div></div></div></div></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">我们在</span><a href="http://mp.weixin.qq.com/s?__biz=MzI3NzE0NjcwMg==&amp;mid=2650121599&amp;idx=1&amp;sn=42b2cfabfb3057ac6c09026a8b9656cd&amp;chksm=f36bb85ec41c31489e461a53e78f2959f0224c87c312724f420265b70e67e4efdae2331155aa&amp;scene=21#wechat_redirect" style="margin: 0px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: normal; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(87, 107, 149); text-decoration: none;" target="_blank">再有人问你Java内存模型是什么，就把这篇文章发给他</a><span style="font-size: 10pt; color: rgb(51, 51, 51);">中分析过：</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">除了引入了时间片以外，由于处理器优化和指令重排等，CPU还可能对输入代码进行乱序执行，比如load-&gt;add-&gt;save 有可能被优化成load-&gt;save-&gt;add 。这就是可能存在有序性问题。</span></div><div style="margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(51, 51, 51);">这里需要注意的是，</span><span style="background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78); background-color: rgb(255, 250, 165);-evernote-highlight:true;">synchronized</span><span style="font-size: 10pt; color: rgb(51, 51, 51);"><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">是无法禁止指令重排和处理器优化的</span><span style="background-color: rgb(255, 255, 255);">。也就是说，</span></span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(51, 51, 51);">无法避免上述提到的问题。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">那么，为什么还说</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">也提供了有序性保证呢？</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">这就要再把有序性的概念扩展一下了。</span></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">Java程序中天然的有序性可以总结为一句话：如果在本线程内观察，所有操作都是天然有序的。如果在一个线程中观察另一个线程，所有操作都是无序的。</span></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">以上这句话也是《深入理解Java虚拟机》中的原句，但是怎么理解呢？周志明并没有详细的解释。这里我简单扩展一下，这其实和</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">as-if-serial语义</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">有关。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">as-if-serial</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">语义的意思指：不管怎么重排序（编译器和处理器为了提高并行度），单线程程序的执行结果都不能被改变。编译器和处理器无论如何优化，都必须遵守</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">as-if-serial</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">语义。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">这里不对</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">as-if-serial语义</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">详细展开了，简单说就是，</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">as-if-serial语义</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">保证了单线程中，指令重排是有一定的限制的，而只要编译器和处理器都遵守了这个语义，那么就可以认为单线程程序是按照顺序执行的。当然，实际上还是有重排的，只不过我们无须关心这种重排的干扰。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(51, 51, 51);">所以呢，由于</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(51, 51, 51);">修饰的代码，同一时间只能被同一线程访问。那么也就是单线程执行的。所以，可以保证其有序性。</span></font></div><div style="background-color: rgb(255, 255, 255); margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin: 0px; padding: 0px; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important;margin-top:10px;margin-bottom:10px"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important;margin-left:0.8em;float:left"><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important"><font style="font-size: 10pt;"><span style="margin: 0.5em 0px 0px; padding: 0px; display: inline-block; vertical-align: top; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="margin: 0px; padding: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(0, 0, 0);"><b>synchronized与锁优化</b></span></span></font></div></div><br/></div><div style="margin:0;padding:0;max-width:100% !important;box-sizing:border-box !important;-webkit-box-sizing:border-box !important;word-wrap:break-word !important;margin-top:10px;margin-bottom:10px"><br/></div><div style="margin: 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="margin: 0px; padding: 0px; letter-spacing: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(0, 0, 0);">前面介绍了</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; letter-spacing: 0px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="margin: 0px; padding: 0px; letter-spacing: 0px; font-size: 10pt; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(0, 0, 0);">的用法、原理以及对并发编程的作用。是一个很好用的关键字。</span></font></div></div></div></div></div></div></div></div><div style="margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><font style="font-size: 10pt;"><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">synchronized</span><span style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(51, 51, 51);">其实是借助Monitor实现的，在加锁时会调用objectMonitor的</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">enter</span><span style="background-color: rgb(255, 255, 255); font-size: 10pt; color: rgb(51, 51, 51);">方法，解锁的时候会调用</span><span style="background: rgb(249, 242, 244); margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">exit</span><span style="font-size: 10pt; color: rgb(51, 51, 51);"><span style="background-color: rgb(255, 255, 255);">方法。</span><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">事实上，只有在JDK1.6之前，synchronized的实现才会直接调用ObjectMonitor的</span></span><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;"><span style="background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">enter</span><span style="font-size: 10pt; color: rgb(51, 51, 51);">和</span><span style="background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; margin: 0px 2px; padding: 2px 4px; border-radius: 4px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; font-size: 10pt; color: rgb(199, 37, 78);">exit</span></span><span style="font-size: 10pt; color: rgb(51, 51, 51);"><span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">，这种锁被称之为重量级锁</span><span>。</span></span></font></div><div style="background-color: rgb(255, 255, 255); margin: 1.5em 0px; padding: 0px; clear: both; min-height: 1em; max-width: 100% !important; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="font-size: 10pt; color: rgb(51, 51, 51);">所以，在JDK1.6中出现对锁进行了很多的优化，进而出现轻量级锁，偏向锁，锁消除，适应性自旋锁，锁粗化(自旋锁在1.4就有，只不过默认的是关闭的，jdk1.6是默认开启的)，这些操作都是为了在线程之间更高效的共享数据 ，解决竞争问题。</span></div></div></div></div></div></div></div><div><br/></div></span>
</div></body></html> 